// index.js
// const app = getApp()
let that = null // 页面this指针变量
let db = wx.cloud.database();

const {
    envList
} = require('../../envList.js');

Page({
    data: {
        showUploadTip: false,
        powerList: [],
        envList,
        selectedEnv: envList[0],
        haveCreateCollection: false,
        inputValue: '', //搜索的关键字
        resultList:[],   //搜索到的结果
        isShow:true     //显示内容
    },
    //搜索框文本内容显示
    inputBind: function(event) {
        this.setData({
            inputValue: event.detail.value
        })
        // console.log('bindInput' + this.data.inputValue)
        //根据输入的数据查询植物
        let key = that.data.inputValue
        if(key==""){
            that.setData({
                isShow:true
            })
            return
        }
        db.collection('plants').where({
            name: db.RegExp({
                regexp: '.*' + key,
                options: 'i',
              })
        }).get().then(res=>{
            if(res.data && res.data.length>0){
                that.setData({
                    isShow:false
                })
            }
            //将搜索的结果显示出来
            that.setData({
                resultList:res.data
            })
        }).catch(err=>{
            console.log(err)
        })

    }, 
    //去植物详情页
    goToDetail:function(e){
        //调转到植物详情页
        wx.navigateTo({
            url: '../plantShow/plantShow?id=' + e.currentTarget.dataset.id ,
        })
    },
    query:function(e){
        console.log(e)
    },

    onClickPowerInfo(e) {
        const index = e.currentTarget.dataset.index;
        const powerList = this.data.powerList;
        powerList[index].showItem = !powerList[index].showItem;
        this.setData({
            powerList
        });
    },

    onChangeShowEnvChoose() {
        wx.showActionSheet({
            itemList: this.data.envList.map(i => i.alias),
            success: (res) => {
                this.onChangeSelectedEnv(res.tapIndex);
            },
            fail(res) {
                console.log(res.errMsg);
            }
        });
    },

    onChangeSelectedEnv(index) {
        if (this.data.selectedEnv.envId === this.data.envList[index].envId) {
            return;
        }
        const powerList = this.data.powerList;
        powerList.forEach(i => {
            i.showItem = false;
        });
        this.setData({
            selectedEnv: this.data.envList[index],
            powerList,
            haveCreateCollection: false
        });
    },

    jumpPage(e) {
        wx.navigateTo({
            // url: `/pages/${e.currentTarget.dataset.page}/index?envId=${this.data.selectedEnv.envId}`,
            url: `/pages/plantListShow/plantListShow?id=${e.currentTarget.dataset.page}`,
        });

        
    },

    onLoad(e){
        that = this
        // return
        //读取所有科类型和属类型

        wx.cloud.callFunction({
            name:'plantTypeKeShuGetAll',
            success:res=>{
                console.log(res)
                let plantsTypeKeList = res.result.list
                that.setData({
                    powerList:plantsTypeKeList
                })
            },
            fail:err=>{
                console.log(err)
            }
        })
        
    }

});